// miniprogram/pages/admin/addRoom.js
const app = getApp()

Page({
    data: {
        roomImgs: [],
        roomDetailImgs: [],
        originalPrice: null,
        currentPrice: null,
        roomUnitsName: null,
        roomUnitsIntro: null,
        isRoomTypeFull: false,
        roomNumber: null,
        roomDetailIntro: null
    },
    onLoad: function(options) {

    },
    checkData() {
        let that = this
        if (that.data.roomImgs.length < 1 || that.data.roomDetailImgs.length < 1) {
            wx.showToast({
                icon: "none",
                title: '请上传房型图片',
            })
            return false
        }
        if (!that.data.originalPrice) {
            wx.showToast({
                icon: "none",
                title: '请输入原价',
            })
            return false
        }
        if (!that.data.currentPrice) {
            wx.showToast({
                icon: "none",
                title: '请输入现价',
            })
            return false
        }
        if (!!!that.data.roomUnitsName) {
            wx.showToast({
                icon: "none",
                title: '请输入房型名称',
            })
            return false
        }
        if (!!!that.data.roomUnitsIntro) {
            wx.showToast({
                icon: "none",
                title: '请输入房间简介',
            })
            return false
        }
        return true
    },
    addRoom() {
        if (!this.checkData()) {
            return
        }
        wx.showLoading({
            title: '上传中',
        })
        let data = this.data
        wx.cloud.callFunction({
            name: 'addRoom',
            data: {
                roomImgs: data.roomImgs,
                originalPrice: data.originalPrice,
                currentPrice: data.currentPrice,
                roomUnitsName: data.roomUnitsName,
                roomUnitsIntro: data.roomUnitsIntro,
                roomDetailImgs: data.roomDetailImgs,
                roomNumber: data.roomNumber,
                roomOrderDateList: JSON.stringify(new Array(+data.roomNumber)),
                isRoomTypeFull: data.isRoomTypeFull,
                roomDetailIntro: data.roomDetailIntro
            },
            success: res => {
                wx.showToast({
                    title: '上传成功',
                })
            },
            fail: err => {
                wx.showToast({
                    icon: "none",
                    title: '上传失败。联系管理员',
                })
                console.log('[云函数] [addRoom] 失败，请检查是否有部署云函数，错误信息：', err)
            },
            complete() {
                wx.hideLoading()
            }
        })
    },
    chooseImage(e) {
        let type = e.currentTarget.dataset.type
        var that = this;
        wx.chooseImage({
            sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function(res) {
                wx.showLoading({
                    mask: true
                })
                wx.cloud.uploadFile({
                    cloudPath: '' + Date.parse(new Date()), // 上传至云端的路径
                    filePath: res.tempFilePaths[0], // 小程序临时文件路径
                    success: res => {
                        // 返回文件 ID
                        that.setData({
                            [type]: that.data[type].concat(res.fileID)
                        })
                        wx.hideLoading()
                        wx.showToast({
                            title: '上传成功',
                        })
                    },
                    fail: error => {
                        wx.showToast({
                            title: error,
                        })
                    }
                })
            }
        })
    },
    myInput(e) {
        let name = e.currentTarget.dataset.name;
        let value = e.detail.detail.value
        this.setData({
            [name]: value
        })
    },
    textAreaInput(e) {
        let name = e.currentTarget.dataset.name;
        let value = e.detail.value
        this.setData({
            [name]: value
        })
    },
    changeSwitch(e) {
        this.setData({
            isRoomTypeFull: e.detail.value
        })
    },
    deleteImage(e) {
        let id = e.currentTarget.id
        let type = e.currentTarget.dataset.type
        let that = this;
        let tmp = that.data[type]
        let index = tmp.indexOf(id);
        wx.showModal({
            title: '提示',
            content: '确认删除这张图片么',
            showCancel: true,
            success: function(res) {
                if (res.confirm) {
                    tmp.splice(index, 1);
                    that.setData({
                        [type]: tmp
                    })
                } else {

                }
            },
        })
    },
})